var urlRoot = '../model/';

var id;

$(function() {
	id = getQueryString("id");
	refreshCallType();
	refreshInfo();
});

var ctData = {};

function refreshCallType() {

	jQuery.ajax({
		type: 'GET',
		contentType: 'application/json',
		url: urlRoot + 'callTypes',
		dataType: 'json',
		async: false,
		success: function(data) {
			if (checkApiErrorInfo(data, "获取调用信息出错")) {
				var items = data.calltype;

				var selStr = "";

				for (var i = 0; i < items.length; i++) {
					var ct = items[i];

					ctData[ct.id] = ct.title;
					selStr += "<option id='ct" + ct.id + "' value=" + ct.id + ">" + ct.title + "</option>";
				}

				$("#call_type").html(selStr);
			}

			if (data.info)
				log(data.info);
		},
		error: function(e) {
			log("返回结果出错： " + JSON.stringify(e));
		}
	});
}

function refreshInfo() {

	if (id < 0) {
		$("#itemTitle").html("新增模型信息");
		return;
	}

	jQuery.ajax({
		type: 'GET',
		contentType: 'application/json',
		url: urlRoot + 'getmodel?id=' + id,
		dataType: 'json',
		async: false,
		success: function(data) {
			if (checkApiErrorInfo(data, "获取模型信息出错")) {
				var model = data.data;

				$("#itemTitle").html("编辑模型信息: [" + id + "] " + model.title);

				$("#input_title").val(model.title);
				$("#input_model").val(model.model);
				$("#input_url").val(model.url);
				$("#input_info").val(model.info);
				$("#input_token").val(model.token);

				$('#ct' + model.type).attr('selected', true);
			}

			if (data.info)
				log(data.info);
		},
		error: function(e) {
			log("返回结果出错： " + JSON.stringify(e));
		}
	});
}

function commit() {

	var param = {}

	var method;
	if (id < 0) {
		method = "createmodel";
	}
	else {
		method = "updatemodel";
		param.id = id;
	}

	param.title = $("#input_title").val();
	param.model = $("#input_model").val();
	param.url = $("#input_url").val();
	param.info = $("#input_info").val();
	param.token = $("#input_token").val();
	param.type = $("#call_type option:selected").val();

	jQuery.ajax({
		type: 'POST',
		contentType: 'application/json',
		url: urlRoot + method,
		data: JSON.stringify(param),
		dataType: 'json',
		async: false,
		success: function(data) {
			if (checkApiErrorInfo(data, "提交模型信息失败")) {
				if (id < 0)
					toPage("./model.html?id=" + data.id);
				else
					refreshInfo();
				log("保存成功");
			}
		},
		error: function(e) {
			showAjaxError(e);
		}
	});
}

function chat() {
	var param = {}
	param.url = $("#input_url").val();
	param.content = $("#input_test").val();
	param.model = $("#input_model").val();
	param.token = $("#input_token").val();
	param.type = $("#call_type option:selected").val();
	if (debug)
		param.dolog = true;

	jQuery.ajax({
		type: 'POST',
		contentType: 'application/json',
		url: urlRoot + "chatTest",
		data: JSON.stringify(param),
		dataType: 'json',
		async: false,
		success: function(data) {

			if (checkApiErrorInfo(data, "对话测试失败")) {
				var result = data.content;
				var cost = data.cost;

				var str = $("#output_test").html();

				if (str.length > 0)
					str += "\n";

				str += "User\t>>> " + param.content;
				str += "\nBot\t>>> " + result + " [" + cost + "]";

				$("#output_test").html(str);
				$("#input_test").val("");

				$('#output_test').scrollTop($('#output_test')[0].scrollHeight);

				if (data.info)
					log(data.info);
				else
					log("");
			}
		},
		error: function(e) {
			showAjaxError(e);
		}
	});
}

function clear() {
	$("#input_test").val("");
	$("#output_test").html("");
}
